<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="zhcms.admin.*" %> 
<%@ page import="zhcms.framework.*" %> 
<%@ include file="include/adminhead.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String action=request.getParameter("atcion");
GyUserLoginValidate gycheck=new GyUserLoginValidate();
int ischeck=gycheck.UserLoginValidate(request, response);
if(ischeck==0)
{
		try{
			out.println("<script language='javascript'>alert('对不起！，您还没有登陆');location.href=\"index.jsp\";</script>");
		}
		catch(Exception e){
			e.printStackTrace();
		}
} 

if(action==null || action.length()<=0  || action.equals("main"))
{
%>
<table width="80%" border="0" cellpadding="0" cellspacing="0" class=TableBorder align="center">
  <form name=a1  method="post" action="?atcion=add">
    <tr align="center" valign="middle"> 
      <th colspan="2" height="22">模板循环标签生成器</th>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td width="20%" align="center">标签名称：</td>
      <td align="left">&nbsp;<input type="text" name="name" size="30" value="">
				</td>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td width="20%" align="center">子段名称：</td>
      <td align="left">&nbsp;文章编号<input type="checkbox" name="fieldname" value="Id"/>&nbsp;栏目编号<input type="checkbox" name="fieldname" value="ColumnCode"/>
      &nbsp;栏目名称<input type="checkbox" name="fieldname" value="ColumnName"/>&nbsp;添加时间<input type="checkbox" name="fieldname" value="AddDate"/>&nbsp;文章标题<input type="checkbox" name="fieldname" value="Title"/>
				&nbsp;浏览数<input type="checkbox" name="fieldname" value="ViewNum"/>&nbsp;图片地址<input type="checkbox" name="fieldname" value="Img"/>
				</td>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td width="20%" align="center">返回行数：</td>
      <td align="left">&nbsp;<input type="text" name="num" size="30" value="10">
				</td>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td width="20%" align="center">所属栏目：</td>
      <td align="left">&nbsp;<select name="ColumnId">
      <option value=0>所有栏目</option>
      			<%
      			GyColumn gyc=new GyColumn();
      			ArrayList<HashMap<String, String>> gytFkOne;
							gytFkOne=new ArrayList<HashMap<String, String>>();
      			gytFkOne=gyc.getAllList();
      			for(int i=0;i<gytFkOne.size();i++)
			      			{
			      		Map<String,String> iiColumninfo=new HashMap<String,String>();
			      		iiColumninfo=gytFkOne.get(i);
			      		int Level=0;
			      		Level=(iiColumninfo.get("ColumnCode").length()/4-1)*3;
			      		out.println("<option value="+iiColumninfo.get("Id"));    		
			      		out.println(">");
			      		
			      		if(iiColumninfo.get("ColumnCode").length()>4)
									 {
									 	out.println("├");
									 }
									
									for(int j=0;j<Level;j++)
									{
										out.println("-");
									}
									out.println(iiColumninfo.get("Name")+"</option>");
			      			}
      			 %>
        </select> <font color=red>*</font></td>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td width="20%" align="center">输出类型：</td>
      <td align="left">&nbsp;<select name="ArticleType">
      <option value="0">所有文章</option>
      <option value="1">文字文章</option>
      <option value="2">图片文章</option>
      </select> <font color=red>*</font>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td width="20%" align="center">文章类型：</td>
      <td align="left">&nbsp;<select name="ArticelNew">
      <option value="0">所有文章</option>
      <option value="1">最新文章</option>
      <option value="2">最热文章</option>
      </select> <font color=red>*</font></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td align="center" valign="middle" colspan="2"><input type="submit" name="Submit" value="生成">&nbsp;<input type="reset" name="Submit2" value="重置"></td>
    </tr>
  </form>
</table>
<%
}
else if(action.equals("add")) 
{
	String name=request.getParameter("name");
	String num=request.getParameter("num");
	String ColumnId=request.getParameter("ColumnId");
	String ArticleType=request.getParameter("ArticleType");
	String ArticelNew=request.getParameter("ArticelNew");
	String[] fieldname = request.getParameterValues("fieldname");
	String HtmlTag="";
	String tempHtmlTag="";
	String JspTag="";
	//第一步，需要生成sql语句
	//System.out.println(fieldname.length);
	String sql="select ";
	for(int i=0;i<fieldname.length;i++)
	{
		sql=sql+fieldname[i]+",";
		tempHtmlTag=tempHtmlTag+"&lt;li&gt;{@#"+name+"."+fieldname[i]+"#@}&lt;/li&gt;";
	}
	//去掉sql中最后一个，
	sql=sql.substring(0, sql.length()-1);
	sql=sql+" from gyhy_content where 1=1 ";
	//栏目
	if(ColumnId.equals("0"))
	{
		//全部文章
		//sql=sql;
	}
	else
	{
		sql=sql+" and ColumnId='"+ColumnId+"' ";
	}
	//输出类型
	if(ArticleType.equals("0"))
	{
		//全部文章
		//sql=sql;
	}
	else if(ArticleType.equals("1"))
	{
		sql=sql+" and IsImg='0' ";
	}
	else if(ArticleType.equals("2"))
	{
		sql=sql+" and IsImg='1' ";
	}
	//文章类型
	if(ArticelNew.equals("0"))
	{
		//全部文章
		sql=sql+" order by id desc ";
	}
	else if(ArticelNew.equals("1"))
	{
	//最新文章
		sql=sql+" order by AddDate desc ";
	}
	else if(ArticelNew.equals("2"))
	{
		sql=sql+" order by ViewNum desc ";
	}
	//文章条数
	sql=sql+ " limit 0 , "+ num;
	//System.out.println(sql);
	
	String ClassNameS="Cl_";//类名
	String VariableName="P_";//变量名
	String arrykey[]={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; 
 	Random x=new Random();
 	//生成一个随机的类名
 for(int i=1;i<=6;i++){
     String temp=null;
     int tempnum=x.nextInt(35);
     temp=arrykey[tempnum];     
     ClassNameS=ClassNameS+temp;
    }
    //生成一个随机的变量名
 for(int i=1;i<=8;i++){
     String temp=null;
     int tempnum=x.nextInt(35);
     temp=arrykey[tempnum];     
     VariableName=VariableName+temp;
    }

 HtmlTag=HtmlTag+"&lt;ul&gt;{@#for:"+name+"#@}<br/>";
 HtmlTag=HtmlTag+tempHtmlTag+"<br/>";
 HtmlTag=HtmlTag+"{@#endfor:"+name+"#@}&lt;/ul&gt;<br/>";
 
 JspTag=JspTag+"ArrayList&lt;HashMap&lt;String, String&gt;&gt; "+VariableName+";<br/>";
 JspTag=JspTag+""+VariableName+"=new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();<br/>";
 JspTag=JspTag+"GyTablseToList "+ ClassNameS +" =new GyTablseToList();<br/>";
 JspTag=JspTag+""+VariableName+"="+ ClassNameS +".getAllList(&quot;"+sql+"&quot;);<br/>";
 JspTag=JspTag+"t.setInForParameter(&quot;"+name+"&quot;, "+VariableName+");<br/>";
%>
<table width="80%" border="0" cellpadding="0" cellspacing="0" class=TableBorder align="center">
    <tr align="center" valign="middle"> 
      <th colspan="2" height="22">模板循环标签生成器</th>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td align="left">&nbsp;<fieldset>
    <legend>生成的html：</legend>
    <%
		out.println(HtmlTag);
	%>
  </fieldset>
				</td>
    </tr>
    <tr bgcolor="#FFFFFF" valign="middle"> 
      <td align="left">&nbsp;
      <fieldset>
    <legend>生成的jsp：</legend>
    <%
		out.println(JspTag);
	%>
  </fieldset>
      </td>
    </tr>
</table>
<%
}
%>
<%@ include file="include/adminfoot.jsp" %>

